package com.aaa.service.impl;

import com.aaa.dao.CinemaDao;
import com.aaa.entity.Cinema;
import com.aaa.service.CinemaService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CinemaServiceImpl implements CinemaService {

    @Autowired
    private CinemaDao cinemaDao;

    public TableData<Cinema> page(String keyword, Integer page, Integer limit) {
        TableData<Cinema> tableData = new TableData<Cinema>();
        //第一步，查询当前页的数据
        Integer offset = (page-1)*limit;
        List<Cinema> data = cinemaDao.listPage(keyword, offset, limit);
        tableData.setData(data);
        //第二步，查询总条数
        Integer count = cinemaDao.count(keyword);
        tableData.setCount(count);
        return tableData;
    }

    public DefaultMsg saveOrUpdate(Cinema cinema) {
        DefaultMsg defaultMsg = new DefaultMsg();
        Integer count;
        if(cinema.getCid()==null){
            count = cinemaDao.save(cinema);
        }else {
            count = cinemaDao.update(cinema);
        }
        if(count==0){
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }

    public DefaultMsg delete(Long cid) {
        DefaultMsg defaultMsg = new DefaultMsg();
        Integer count = cinemaDao.delete(cid);
        if(count==0){
            defaultMsg.setError("操作失败");
        }
        return defaultMsg;
    }
}
